import { reqLogin } from '@/api/user'
import { setToken, getToken, removeToken } from '@/utils/storage'
export default {
  namespaced: true,
  state: {
    // 优先从本地存储里拿出来tokenInfo，没有的话再给初始值
    tokenInfo: getToken() || {}
  },
  mutations: {
    setTokenFn (state, data) {
      state.tokenInfo = data
      console.log('存', state.tokenInfo)
      setToken(data)
      const res = getToken()
      console.log('取', res)
    },
    // 移出tokenInfo的信息, 恢复成空对象
    removeTokenInfo (state) {
      state.tokenInfo = {}
      // 更新到本地, 本地可以清掉token信息
      removeToken()
    }
  },
  actions: {
    async login (context, formList) {
      console.log(formList)
      const { data } = await reqLogin(formList)
      console.log(data)
      if (data.message === 'OK') context.commit('setTokenFn', data.data)
    }
  }
}
